package com.emep.emeptime.service.aqi;

import com.emep.emeptime.entity.AreaAqiMoniter;
import com.emep.emeptime.entity.api.AqiNumberOfDays;
import com.emep.emeptime.repository.AreaAqiMoniterRepository;
import com.emep.emeptime.repository.aqi.AqiNumberOfDaysRepository;
import com.emep.emeptime.service.AreaAqiMoniterService;
import com.emep.emeptime.utils.DateTimeUtil;
import com.emep.emeptime.utils.DateUtils;
import com.emep.emeptime.utils.StringUtils;
import com.sun.javafx.scene.control.skin.VirtualFlow;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author xieyang
 * @version V1.0
 * @Description: 优良天数，重污染天数，pm2.5已达数值 业务层
 * @date 2018/3/30  11:12
 */
@Service
@Slf4j
public class AqiNumberOfDaysService {

    @Resource
    private AqiNumberOfDaysRepository aqiNumberOfDaysRepository;

    @Autowired
    private AreaAqiMoniterService areaAqiMoniterService;


    private static final int DEF_DIV_SCALE = 10;

    /**
     *
     * 新增优良天数，重污染天数，pm2.5已达数值
     *
     * @return
     * @throws Exception
     */
    public void save() throws ParseException {
        AqiNumberOfDays aqiNumberOfDays = new AqiNumberOfDays();
        //一天api数据
        Double HeavyPollution = 0.0;
        List<AreaAqiMoniter> areaAqiMoniters =  areaAqiMoniterService.findall(DateTimeUtil.getStartDate(DateTimeUtil.getDateTimeForString(new Date(),DateTimeUtil.DATE_DEFAULT_FORMAT)),
                DateTimeUtil.getEndDate(DateTimeUtil.getDateTimeForString(new Date(),DateTimeUtil.DATE_DEFAULT_FORMAT)));
        if(areaAqiMoniters != null && !areaAqiMoniters.isEmpty()){
            for(AreaAqiMoniter areaAqiMoniter:areaAqiMoniters){
                if(!StringUtils.isEmptyBool(areaAqiMoniter.getAqi())){
                    HeavyPollution = HeavyPollution+Integer.valueOf(areaAqiMoniter.getAqi());
                }
            }
        }
        /*******************************************************************************************
         *                                 计算pm2.5
         ******************************************************************************************/
        Double pm25 = 0.0;
        //全年的pm2.5数据
        List<AreaAqiMoniter> areaAqiMoniters1 = getYerAll();
        if (areaAqiMoniters1 != null && !areaAqiMoniters1.isEmpty()){
            for (AreaAqiMoniter areaAqiMoniter:areaAqiMoniters1){
                pm25 = pm25 + Double.valueOf(areaAqiMoniter.getPm25());
            }
        }
        /*******************************************************************************************
         *                                 计算重污染天数和优良
         ******************************************************************************************/
        Double reslt  = new Double(new BigDecimal(HeavyPollution.toString()).divide(
                new BigDecimal(Double.valueOf(areaAqiMoniters.size()).toString()),
                DEF_DIV_SCALE, BigDecimal.ROUND_HALF_UP)
                .doubleValue());
        List<AqiNumberOfDays> aqiNumberOfDays1 =  aqiNumberOfDaysRepository.findAll();
        if(aqiNumberOfDays1.size() != 0){
            int newDateYear = 0;
            for(int i = 0;i<aqiNumberOfDays1.size();i++){
                aqiNumberOfDays = aqiNumberOfDays1.get(i);
                //当前年份计算
                if(aqiNumberOfDays.getYear().equals(DateTimeUtil.getNowDateTimeForString("yyyy"))){
                    newDateYear++;
                    aqiNumberOfDays.setPm25Max(pm25/areaAqiMoniters1.size());
                    storage(reslt,aqiNumberOfDays);
                }
            }
            //新的一年，重新创建一条
            if(newDateYear == 0){
                aqiNumberOfDays = new AqiNumberOfDays();
                aqiNumberOfDays.setPm25Max(pm25/areaAqiMoniters1.size());
                storage(reslt,aqiNumberOfDays);
            }
        }else {
            //项目第一次运行
            aqiNumberOfDays = new AqiNumberOfDays();
            aqiNumberOfDays.setPm25Max(pm25 / areaAqiMoniters1.size());
            storage(reslt,aqiNumberOfDays);
        }
    }

    /**
     * 取一年数据
     * @return
     */
    public List<AreaAqiMoniter> getYerAll() throws ParseException {
        return areaAqiMoniterService.findall(DateTimeUtil.getYer(),String.valueOf(System.currentTimeMillis()));
    }

    /**
     *
     * 计算保存
     */
    public void storage(Double reslt,AqiNumberOfDays aqiNumberOfDays) throws ParseException {
        if(reslt <= 100.0){
            aqiNumberOfDays.setExcellentMax(aqiNumberOfDays.getExcellentMax()+1);
        }
        //重污染天数（以一天aqi平均数计算）
        if(reslt > 200.0){
            aqiNumberOfDays.setPollutionMax(aqiNumberOfDays.getPollutionMax()+1);
        }
        aqiNumberOfDays.setCreateDate(String.valueOf(System.currentTimeMillis()));
        aqiNumberOfDays.setYear(DateTimeUtil.getYear());
        String date = DateTimeUtil.getEndDate(DateTimeUtil.getDateTimeForString(new Date(),DateTimeUtil.DATE_DEFAULT_FORMAT));
        aqiNumberOfDays.setByday(date);
        AqiNumberOfDays aqiNumberOfDays2 = aqiNumberOfDaysRepository.save(aqiNumberOfDays);
        if (aqiNumberOfDays2 != null && !StringUtils.isEmpty(aqiNumberOfDays2.getId())){
            log.info("************ 计算更新成功*********");
        }else {
            log.error("************ 计算更新失败*********");
        }
    }
}
